<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="//unpkg.com/layui@2.6.7/dist/css/layui.css">
    <!-- 引入 layui.js -->
    <script src="//unpkg.com/layui@2.6.7/dist/layui.js"></script>

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>高级应用：制作一个多文件列表</legend>
</fieldset>

<div class="layui-upload">
    <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
    <div class="layui-upload-list" style="max-width: 1000px;">
        <table class="layui-table">
            <colgroup>
                <col>
                <col width="150">
                <col width="260">
                <col width="150">
            </colgroup>
            <thead>
            <tr>
                <th>文件名</th>
                <th>大小</th>
                <th>上传进度</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="demoList"></tbody>
        </table>
    </div>
    <button type="button" class="layui-btn" id="testListAction">开始上传</button>
</div>
<br>
<br>
<br>
<hr>
<table id="demo" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="xiazai">下载</a>
    <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>


</script>
<script th:inline="none">
    layui.use(['upload', 'element', 'layer', 'table'], function () {
        var $ = layui.jquery
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer;

        //演示多文件列表
        var uploadListIns = upload.render({
            elem: '#testList'
            , elemList: $('#demoList') //列表元素对象
            , url: 'http://localhost:8080/upload'
            , accept: 'file'
            , multiple: true
            , number: 3
            , auto: false
            , bindAction: '#testListAction'
            , choose: function (obj) {
                var that = this;
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                        , '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    that.elemList.append(tr);
                    element.render('progress'); //渲染新加的进度条组件
                });
            }
            , done: function (res, index, upload) { //成功的回调
                var that = this;
                if (res.code == 0) { //上传成功
                    var tr = that.elemList.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(3).html(''); //清空操作
                    delete this.files[index]; //删除文件队列已经上传成功的文件
                    location.reload()
                    return;
                }
                this.error(index, upload);
            }
            , allDone: function (obj) { //多文件上传完毕后的状态回调
                console.log(obj)
            }
            , error: function (index, upload) { //错误回调
                var that = this;
                var tr = that.elemList.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
            , progress: function (n, elem, e, index) {
                element.progress('progress-demo-' + index, n + '%'); //执行进度条。n 即为返回的进度百分比
            }
        });
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#demo'
            , height: 312
            , url: 'http://localhost:8080/list' //数据接口
            , page: false //开启分页
            , cols: [[
                {field: 'fileid', title: 'ID', width: 180, sort: true, fixed: 'left'}
                , {field: 'filename', title: '文件名', width: 180}
                , {field: 'fileurl', title: '地址', width: 180, sort: true}
                , {field: 'createtime', title: '时间', width: 180}
                , {field: 'upperson', title: '操作人', width: 200}
                , {fixed: 'right', width: 250, align: 'center', toolbar: '#barDemo'}
            ]]
        });

        //工具条事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'xiazai') { //查看
                location.href = "http://localhost:8080/dow?fileid=" + data.fileid
                // //do somehing
                // console.log(data.fileid)
                // $.ajax({
                //     url:"http://localhost:8080/dow?fileid="+data.fileid,
                //     type:"post",
                //     /** 9
                //      *必须false才会自动加上正确的Content-Type 10
                //      */
                //     contentType: false,
                //     /** 13
                //      * 必须false才会避开jQuery对 formdata 的默认处理 14
                //      * XMLHttpRequest会对 formdata 进行正确的处理 15
                //      */
                //     processData: false,
                //     success:function (reData){
                //         console.log(reData);
                //     }
                // })
            } else if (layEvent === 'delete') {

                $.get("http://localhost:8080/del?fileid=" + data.fileid, function (result) {
                    $("div").html(result);
                });
                location.reload()
                //location.href = "http://localhost:8080/del?fileid="+data.fileid
            }
        });
    })
</script>
</body>

</html>
